home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / fgdemo10.zip / COMMON.C < prev    next >
Text File  |  1991-10-02  |  14KB  |  534 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  common.c -- routines common to many modules                         *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8.  
  9. int background;
  10.  
  11. int hidden;
  12. int visual;
  13. int vpage;
  14.  
  15. int xlimit;
  16. int ylimit;
  17.  
  18. int old_mode;
  19. int mode;
  20.  
  21. int mode06;
  22. int mode11;
  23. int mode14;
  24. int mode16;
  25.  
  26. int mode06_avail;
  27. int mode11_avail;
  28. int mode14_avail;
  29. int mode16_avail;
  30.  
  31. int mouse;
  32. int xmouse;
  33. int ymouse;
  34. int buttons;
  35.  
  36. char string[80];
  37.  
  38. /**********************************************************************\
  39. *                                                                      *
  40. *  abort_program -- called when the program can't be run               *
  41. *                                                                      *
  42. \**********************************************************************/
  43.  
  44. void abort_program(abort_code)
  45. int abort_code;
  46. {
  47.    /* reset the mode to what it was (probably mode 3) */
  48.  
  49.    fg_setmode(old_mode);
  50.    fg_reset();
  51.  
  52.    /* print a line that will stay on the screen after the program exits */
  53.  
  54.    if (abort_code == 0)
  55.      printf("\nYour system does not have enough memory to run this program.\n");
  56.    else if (abort_code == 1)
  57.      printf("\nThe font file is missing.\n");
  58.    exit(0);
  59. }
  60.  
  61. /**********************************************************************\
  62. *                                                                      *
  63. *  draw_box -- draw a line rectangle in screen space                   *
  64. *                                                                      *
  65. \**********************************************************************/
  66.  
  67. void draw_box(xmin,xmax,ymin,ymax)
  68. int xmin, xmax, ymin, ymax;
  69. {
  70.    fg_move(xmin,ymin);
  71.    fg_draw(xmax,ymin);
  72.    fg_draw(xmax,ymax);
  73.    fg_draw(xmin,ymax);
  74.    fg_draw(xmin,ymin);
  75. }
  76.  
  77. /**********************************************************************\
  78. *                                                                      *
  79. *  draw_tbox -- draw non-graphics single line box                      *
  80. *                                                                      *
  81. \**********************************************************************/
  82.  
  83. void draw_tbox(col1,col2,row1,row2)
  84. int col1, col2, row1, row2;
  85. {
  86.    /* we are working in character space, so use rows and columns */
  87.  
  88.    register int row, col;
  89.  
  90.    /* these are the ASCII characters to make the single line box */
  91.  
  92.    static char *ul = "┌";
  93.    static char *ur = "┐";
  94.    static char *ll = "└";
  95.    static char *lr = "┘";
  96.    static char *horz = "─";
  97.    static char *vert = "│";
  98.  
  99.    /* draw the sides of the box */
  100.  
  101.    for (row = row1+1; row < row2; row++)
  102.    {
  103.       put_tstring(vert,row,col1);
  104.       put_tstring(vert,row,col2);
  105.    }
  106.  
  107.    /* draw the top and the bottom of the box */
  108.  
  109.    for (col = col1+1; col < col2; col++)
  110.    {
  111.       put_tstring(horz,row1,col);
  112.       put_tstring(horz,row2,col);
  113.    }
  114.  
  115.    /* put in the corners */
  116.  
  117.    put_tstring(ul,row1,col1);
  118.    put_tstring(ur,row1,col2);
  119.    put_tstring(ll,row2,col1);
  120.    put_tstring(lr,row2,col2);
  121. }
  122.  
  123. /**********************************************************************\
  124. *                                                                      *
  125. *  exists -- does the file exist?                                      *
  126. *                                                                      *
  127. \**********************************************************************/
  128.  
  129. exists(filename)
  130. char *filename;
  131. {
  132.    if (access(filename,0) == 0)
  133.       return(TRUE);
  134.    else
  135.       return(FALSE);
  136. }
  137.  
  138. /**********************************************************************\
  139. *                                                                      *
  140. *  flushkey -- flush out the keystroke buffer and the mouse buttons    *
  141. *                                                                      *
  142. \**********************************************************************/
  143.  
  144. void flushkey()
  145. {
  146.    int dummy;
  147.    unsigned char key, aux;
  148.  
  149.    if (mouse)
  150.    {
  151.       fg_mousebut(1,&dummy,&dummy,&dummy);
  152.       fg_mousebut(2,&dummy,&dummy,&dummy);
  153.    }
  154.  
  155.    do
  156.       fg_intkey(&key,&aux);
  157.    while (key+aux > 0);
  158. }
  159.  
  160. /**********************************************************************\
  161. *                                                                      *
  162. *          initialize -- initialize the Fastgraph environment          *
  163. *                                                                      *
  164. \**********************************************************************/
  165.  
  166. void initialize()
  167. {
  168.    register int page;
  169.  
  170.    /* Check which video modes are available */
  171.  
  172.    mode06_avail = fg_testmode(6,PAGES);
  173.    mode11_avail = fg_testmode(11,PAGES);
  174.    mode14_avail = fg_testmode(14,PAGES);
  175.    mode16_avail = fg_testmode(16,PAGES);
  176.  
  177.    /* Abort the program if none of the video modes are available */
  178.  
  179.    if (!(mode06_avail | mode11_avail | mode14_avail | mode16_avail))
  180.       abort_program(0);
  181.  
  182.    /* Save the current video mode */
  183.  
  184.    old_mode = fg_getmode();
  185.  
  186.    /* Propose the default video mode */
  187.  
  188.    if (mode16_avail)
  189.       mode = 16;
  190.    else if (mode14_avail)
  191.       mode = 14;
  192.    else if (mode11_avail)
  193.       mode = 11;
  194.    else if (mode06_avail)
  195.       mode = 6;
  196.  
  197.    /* Let the user choose one of the supported modes */
  198.  
  199.    mode = select_mode();
  200.  
  201.    /* Switch to the selected video mode */
  202.  
  203.    fg_setmode(mode);
  204.  
  205.    /* Allocate virtual pages as needed */
  206.  
  207.    for (page = 1; page < PAGES; page++)
  208.       fg_allocate(page);
  209.  
  210.    /* Set up visual and hidden pages */
  211.  
  212.    fg_sethpage(1);
  213.    visual = 0;
  214.    hidden = 1;
  215.    background = 0;
  216.  
  217.    /* Determine maximum screen space coordinates */
  218.  
  219.    xlimit = fg_getmaxx();
  220.    ylimit = fg_getmaxy();
  221.  
  222.    /* For Hercules mode, we'll only use the first 640 of 720 columns */
  223.  
  224.    if (xlimit > 639) xlimit = 639;
  225.  
  226.    /* For 640x350 EGA mode, use only as much vertical resolution as Hercules */
  227.  
  228.    if (ylimit > 347) ylimit = 347;
  229.  
  230.    /* Initialize the mouse, if present */
  231.  
  232.    init_mouse();
  233. }
  234.  
  235. /**********************************************************************\
  236. *                                                                      *
  237. *  init_mouse -- initialize the mouse if present                       *
  238. *                                                                      *
  239. \**********************************************************************/
  240.  
  241. void init_mouse()
  242. {
  243.    if (fg_mouseini() > 0)
  244.    {
  245.       mouse = TRUE;
  246.       fg_mouselim(6,xlimit-5,0,ylimit);
  247.       xmouse = xlimit / 2;
  248.       ymouse = ylimit / 2;
  249.       fg_mousemov(xmouse,ymouse);
  250.       fg_mousevis(ON);
  251.    }
  252.    else
  253.       mouse = FALSE;
  254. }
  255.  
  256. /**********************************************************************\
  257. *                                                                      *
  258. *  printer_ready -- is the printer on line?                            *
  259. *                                                                      *
  260. \**********************************************************************/
  261.  
  262. printer_ready()
  263. {
  264.    static union REGS registers;
  265.  
  266.    registers.h.ah = 2;
  267.    registers.x.dx = 0;
  268.    int86(0x17,®isters,®isters);
  269.    return(registers.h.ah == 0x90);
  270. }
  271.  
  272. /**********************************************************************\
  273. *                                                                      *
  274. *  scale -- the y in the low-res modes is 4/7th the y in hi-res modes  *
  275. *                                                                      *
  276. \**********************************************************************/
  277.  
  278. scale(y)
  279. int y;
  280. {
  281.    if (mode06 || mode14)
  282.       return((y * 4) / 7);
  283.    else
  284.       return(y);
  285. }
  286.  
  287. /**********************************************************************\
  288. *